package com.qingdai.service.impl;

import com.github.yulichang.query.MPJLambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.qingdai.emuns.*;
import com.qingdai.mapper.AddressMapper;
import com.qingdai.service.AddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 详细地址 服务实现类
 * </p>
 *
 * @author LiuZiMing
 * @since 2024-01-26
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {
    @Resource
    private AddressMapper addressMapper;

    @Override
    public List<AddressDTO> getAddressDTO() {
        //        ProvinceName
        MPJLambdaWrapper<Address> wrapper = new MPJLambdaWrapper<>();
        wrapper
                .selectAll(Address.class)
                .selectAs(Places::getCname, AddressDTO::getProvinceName)
                .leftJoin(Places.class, Places::getId, Address::getProvinceId);
//        CityName
        MPJLambdaWrapper<Address> wrapper2 = new MPJLambdaWrapper<>();
        List<AddressDTO> addressDTOList = addressMapper.selectJoinList(AddressDTO.class, wrapper);
        wrapper2
                .selectAll(Address.class)
                .selectAs(Places::getCname, AddressDTO::getCityName)
                .leftJoin(Places.class, Places::getId, Address::getCityId);
        List<AddressDTO> addressDTOList2 = addressMapper.selectJoinList(AddressDTO.class, wrapper2);
//        DistrictName
        MPJLambdaWrapper<Address> wrapper3 = new MPJLambdaWrapper<>();
        wrapper3
                .selectAll(Address.class)
                .selectAs(Places::getCname, AddressDTO::getDistrictName)
                .leftJoin(Places.class, Places::getId, Address::getDistrictId);
        List<AddressDTO> addressDTOList3 = addressMapper.selectJoinList(AddressDTO.class, wrapper3);

        for (int i = 0; i < addressDTOList.size(); i++) {
            addressDTOList.get(i).setCityName(addressDTOList2.get(i).getCityName());
            addressDTOList.get(i).setDistrictName(addressDTOList3.get(i).getDistrictName());
        }

        return addressDTOList;
    }

    @Override
    public List<AddressDTO> getAddressDTOByUserId(Integer userId) {
//        ProvinceName
        MPJLambdaWrapper<Address> wrapper = new MPJLambdaWrapper<>();
        wrapper.eq("t.user_id", userId)
                .selectAll(Address.class)
                .selectAs(Places::getCname, AddressDTO::getProvinceName)
                .leftJoin(Places.class, Places::getId, Address::getProvinceId);
//        CityName
        MPJLambdaWrapper<Address> wrapper2 = new MPJLambdaWrapper<>();
        List<AddressDTO> addressDTOList = addressMapper.selectJoinList(AddressDTO.class, wrapper);
        wrapper2
                .eq("t.user_id", userId)
                .selectAll(Address.class)
                .selectAs(Places::getCname, AddressDTO::getCityName)
                .leftJoin(Places.class, Places::getId, Address::getCityId);
        List<AddressDTO> addressDTOList2 = addressMapper.selectJoinList(AddressDTO.class, wrapper2);
//        DistrictName
        MPJLambdaWrapper<Address> wrapper3 = new MPJLambdaWrapper<>();
        wrapper3
                .eq("t.user_id", userId)
                .selectAll(Address.class)
                .selectAs(Places::getCname, AddressDTO::getDistrictName)
                .leftJoin(Places.class, Places::getId, Address::getDistrictId);
        List<AddressDTO> addressDTOList3 = addressMapper.selectJoinList(AddressDTO.class, wrapper3);

        for (int i = 0; i < addressDTOList.size(); i++) {
            addressDTOList.get(i).setCityName(addressDTOList2.get(i).getCityName());
            addressDTOList.get(i).setDistrictName(addressDTOList3.get(i).getDistrictName());
        }

        return addressDTOList;
    }


}
